package yxk.task.moka;

import com.alibaba.fastjson.JSONObject;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.exception.KDException;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.schedule.executor.AbstractTask;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.sdk.plugin.Plugin;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 岗位新增定时任务
 */
public class MoKaNewPositionTask extends AbstractTask implements Plugin {
    private static final Log LOG = LogFactory.getLog(MoKaNewPositionTask.class);
    @Override
    public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
        LOG.info("====岗位同步已经入=====");
        MoKaService service=new MoKaService();
        Map<String, JSONObject> mapPosition = service.getMapPosition();
        String moKaToKen = service.getMoKaToKen();
        LOG.info("====moKaToKen:{}",moKaToKen);
        if(moKaToKen != null) {
            List<DynamicObject> list = new ArrayList<>();
            for (String key : mapPosition.keySet()) {
                QFilter qFilter = new QFilter("number", QCP.equals, key);
                DynamicObject[] load = BusinessDataServiceHelper.load("hbpm_positionhr", "number,nwg5_mokaid,school_mokaid,iscurrentversion", qFilter.toArray());
                //社招
                Boolean bool = false;//是否已同步摩卡   true：已同步  ， false : 无同步
                Boolean bool2 = false;//同步摩卡是否有更新   false：有更新；  true：无更新
                //校招
                Boolean school=false;
                Boolean school2=false;
                String mokaid = "";
                String school_MoKaId = "";
                DynamicObject positionhr = null;
                for (DynamicObject row : load) {
                    //社招摩卡ID
                    String nwg5_mokaid = row.getString("nwg5_mokaid");
                    //校招摩卡ID
                    String schoolMokaid = row.getString("school_mokaid");
                    boolean iscurrentversion = row.getBoolean("iscurrentversion");
                    if (nwg5_mokaid != null && !"".equals(nwg5_mokaid)) {
                        bool = true;
                        mokaid = nwg5_mokaid;
                    }
                    if (iscurrentversion) {
                        positionhr = row;
                        if (nwg5_mokaid != null && !"".equals(nwg5_mokaid)) {
                            bool2 = true;
                        }
                        if (schoolMokaid != null && !"".equals(schoolMokaid)) {
                            school2 = true;
                        }
                    }

                    if (schoolMokaid != null && !"".equals(schoolMokaid)) {
                        school = true;
                        school_MoKaId = nwg5_mokaid;
                    }
                }
                Boolean updateBool=false;//是否 有变更单据
                JSONObject jsonObject = mapPosition.get(key);
                LOG.info("MoKaNewPositionTask.execute.bool:[{}],bool2:[{}],mokaid:[{}]", bool, bool2, mokaid);
                LOG.info("MoKaNewPositionTask.execute.jsonObject:{}", jsonObject);
                //社招同步更新
                if (bool && !bool2) {
                    String jobId = service.updateMoKaHRPosition(jsonObject, moKaToKen, mokaid);
                    LOG.info("MoKaNewPositionTask.execute.kaHRPosition:{}", jobId);
                    if (jobId != null && positionhr != null) {
                        positionhr.set("nwg5_mokaid", jobId);
                        updateBool=true;
                    }
                } else if (!bool) {
                    String kaHRPosition = service.setMoKaHRPosition(jsonObject, moKaToKen);
                    LOG.info("MoKaNewPositionTask.execute.kaHRPosition:{}", kaHRPosition);
                    if (kaHRPosition != null && positionhr != null) {
                        positionhr.set("nwg5_mokaid", kaHRPosition);
                        updateBool=true;
                    }
                }

                if (school && !school2) {
                    String jobId = service.updateMoKaHRPosition(jsonObject, moKaToKen, school_MoKaId);
                    LOG.info("MoKaNewPositionTask.execute.kaHRPosition:{}", jobId);
                    if (jobId != null && positionhr != null) {
                        positionhr.set("school_mokaid", jobId);
                        updateBool=true;
                    }
                } else if (!school) {
                    String kaHRPosition = service.setSchoolMoKaHRPosition(jsonObject, moKaToKen);
                    LOG.info("MoKaNewPositionTask.execute.kaHRPosition:{}", kaHRPosition);
                    if (kaHRPosition != null && positionhr != null) {
                        positionhr.set("school_mokaid", kaHRPosition);
                        updateBool=true;
                    }
                }

                if(updateBool) {
                    list.add(positionhr);
                }

            }
            DynamicObject[] dynamicObjects = new DynamicObject[list.size()];
            for (int i = 0; i < list.size(); i++) {
                dynamicObjects[i] = list.get(i);
            }
            SaveServiceHelper.update(dynamicObjects);
        }
    }
}